package com.ss.android.newmedia.activity.browser;

import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.UIUtils;
import com.bytedance.frameworks.b.a.ComponentsManifest;
import com.ss.android.article.browser.R;
import com.ss.android.common.util.ImmersedStatusBarHelper;
import com.ss.android.newmedia.a.v;
import com.ss.android.newmedia.a.w;
import com.ss.android.newmedia.activity.FragmentActivity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

public class BrowserActivity extends FragmentActivity implements v, w, d {
    public enum OperationButton {
        REFRESH(R.id.refresh, "refresh", R.string.ee),
        COPYLINK(R.id.af5, "copylink", R.string.ec),
        OPEN_WITH_BROWSER(R.id.af6, "openwithbrowser", R.string.ed),
        SHARE(R.id.af7, "share", R.string.ef);

        public int mId;
        public String mKey;
        public int mTitleRes;

        private OperationButton(int i, String str, int i2) {
            this.mId = i;
            this.mKey = str;
            this.mTitleRes = i2;
        }
    }

    public interface a {
        void a(String arg1);

        void c();

        boolean d();
    }

    private String a;
    String b;
    WeakReference c;
    ImageView d;
    List e;
    PopupMenu.OnMenuItemClickListener f;
    private boolean g;
    private boolean h;
    private String i;
    private String j;
    private boolean k;
    private int l;
    private int m;
    private boolean n;
    private View o;
    private TextView p;
    private View.OnClickListener q;

    public BrowserActivity() {

        this.g = false;
        this.k = false;
        this.l = 0;
        this.m = 1;
        this.q = new com.ss.android.newmedia.activity.browser.a(this);
        this.f = new b(this);
    }

    static TextView a(BrowserActivity arg1) {
        return arg1.mRightBtn;
    }

    public e a() {
        Object v0 = com.bytedance.frameworks.b.a.c.a(com.ss.android.dd.class);
        e v0_1 = v0 != null ? ((com.ss.android.dd) v0).b() : null;
        return v0_1;
    }

    public final void a(int arg2) {
        int v0 = 4;
        switch (arg2) {
            case 0: {
                this.setRequestedOrientation(v0);
                break;
            }
            case 1: {
                this.setRequestedOrientation(1);
                break;
            }
            case 2: {
                this.setRequestedOrientation(0);
                break;
            }
            default: {
                this.setRequestedOrientation(v0);
                break;
            }
        }
    }

    public final void a(String arg7) {
        int v5 = R.color.an;
        int v4 = R.color.ad;
        Drawable v3 = null;
        if (!android.support.design.a.isTextEmpty(arg7)) {
            Drawable v0 = this.p.getCompoundDrawables()[0];
            if (v0 != null) {
                if ("white".equals(arg7)) {
                    v0.setColorFilter(this.getResources().getColor(v4), PorterDuff.Mode.SRC_IN);
                } else if ("black".equals(arg7)) {
                    v0.setColorFilter(this.getResources().getColor(v5), PorterDuff.Mode.SRC_IN);
                }

                this.p.setCompoundDrawablesWithIntrinsicBounds(v0, v3, v3, v3);
            }

            v0 = this.d.getDrawable();
            if (v0 != null) {
                if ("white".equals(arg7)) {
                    v0.setColorFilter(this.getResources().getColor(v4), PorterDuff.Mode.SRC_IN);
                } else if ("black".equals(arg7)) {
                    v0.setColorFilter(this.getResources().getColor(v5), PorterDuff.Mode.SRC_IN);
                }

                this.d.setImageDrawable(v0);
            }

            this.a = arg7;
        }
    }

    public final void a(List arg1) {
        this.e = arg1;
    }

    public final void a(boolean arg2) {
        if (this.h) {
            ImmersedStatusBarHelper.setUseLightStatusBar(this.getWindow(), arg2);
        }
    }

    public final void b(String arg6) {
        int v4 = 8;
        if (!android.support.design.a.isTextEmpty(arg6)) {
            if ("back_arrow".equals(arg6)) {
                this.p.setCompoundDrawablesWithIntrinsicBounds(com.ss.android.article.browser.R.drawable.gj, 0, 0, 0);
            } else if ("close".equals(arg6)) {
                this.p.setCompoundDrawablesWithIntrinsicBounds(com.ss.android.article.browser.R.drawable.ic_close_selector, 0, 0, 0);
                this.g = true;
                UIUtils.setVisibility(this.d, v4);
            } else if ("down_arrow".equals(arg6)) {
                this.p.setCompoundDrawablesWithIntrinsicBounds(com.ss.android.article.browser.R.drawable.c3, 0, 0, 0);
                this.g = true;
                UIUtils.setVisibility(this.d, v4);
            }
        }
    }

    public boolean b() {
        return false;
    }

    public final void c() {
        UIUtils.setVisibility(this.mNightModeOverlay, 8);
    }

    public final void c(String arg8) {
        int v6 = 12;
        int v5 = 11;
        int v4 = 10;
        int v3 = 9;
        if (!android.support.design.a.isTextEmpty(arg8)) {
            ViewGroup.LayoutParams v0 = this.p.getLayoutParams();
            if ("top_left".equals(arg8)) {
                ((RelativeLayout.LayoutParams) v0).addRule(v3);
                ((RelativeLayout.LayoutParams) v0).addRule(v4);
                ((RelativeLayout.LayoutParams) v0).addRule(v5, 0);
                ((RelativeLayout.LayoutParams) v0).addRule(v6, 0);
            } else if ("top_right".equals(arg8)) {
                ((RelativeLayout.LayoutParams) v0).addRule(v5);
                ((RelativeLayout.LayoutParams) v0).addRule(v4);
                ((RelativeLayout.LayoutParams) v0).addRule(v3, 0);
                ((RelativeLayout.LayoutParams) v0).addRule(v6, 0);
            } else if ("bottom_left".equals(arg8)) {
                ((RelativeLayout.LayoutParams) v0).addRule(v3);
                ((RelativeLayout.LayoutParams) v0).addRule(v6);
                ((RelativeLayout.LayoutParams) v0).addRule(v5, 0);
                ((RelativeLayout.LayoutParams) v0).addRule(v4, 0);
            } else if ("bottom_right".equals(arg8)) {
                ((RelativeLayout.LayoutParams) v0).addRule(v5);
                ((RelativeLayout.LayoutParams) v0).addRule(v6);
                ((RelativeLayout.LayoutParams) v0).addRule(v3, 0);
                ((RelativeLayout.LayoutParams) v0).addRule(v4, 0);
            }

            this.p.setLayoutParams(v0);
        }
    }

    final WebView d() {
        Object v1 = null;
        Object v0 = this.c != null ? this.c.get() : v1;
        WebView v0_1 = v0 == null || !((e) v0).isActive() ? ((WebView) v1) : ((e) v0).n();
        return v0_1;
    }

    public final void e() {
        this.g = true;
    }

    public ImmersedStatusBarHelper.ImmersedStatusBarConfig getImmersedStatusBarConfig() {
        int v5 = R.color.status_bar_color_detail;
        int v4 = 23;
        ImmersedStatusBarHelper.ImmersedStatusBarConfig v0 = new ImmersedStatusBarHelper.ImmersedStatusBarConfig();
        if (this.h) {
            v0.mFitsSystemWindows = true;
            v0.setStatusBarColor(com.ss.android.article.browser.R.color.status_bar_color_transparent);
        }

        if (!android.support.design.a.isTextEmpty(this.i) && Build.VERSION.SDK_INT >= v4) {
            if ("black".equals(this.i)) {
                v0.mIsUseLightStatusBar = true;
            } else if ("white".equals(this.i)) {
                v0.mIsUseLightStatusBar = false;
            }
        }

        if (!this.h) {
            if (!android.support.design.a.isTextEmpty(this.j) && Build.VERSION.SDK_INT >= v4) {
                if ("black".equals(this.j)) {
                    v0.setStatusBarColor(com.ss.android.article.browser.R.color.status_bar_color_black);
                } else if ("white".equals(this.j)) {
                    v0.setStatusBarColor(v5);
                } else {
                }

                return v0;
            }

            v0.setStatusBarColor(v5);
        }

        return v0;
    }

    public final int getLayout() {
        return R.layout.browser_activity;
    }

    public void init() {
        JSONObject v7_1 = null;
        String v27;
        boolean v3;
        boolean v2;
        this.p = (TextView) this.findViewById(R.id.browser_back_btn);
        this.p.setOnClickListener(this.q);
        this.o = this.findViewById(com.ss.android.article.browser.R.id.browser_title_bar_shadow);
        this.d = (ImageView) this.findViewById(R.id.close_all_webpage);
        this.d.setOnClickListener(this.q);
        Intent v23 = this.getIntent();
        String v22 = null;
        boolean v21 = false;
        String v20 = null;
        long v18 = 0;
        String v17 = null;
        boolean v16 = false;
        int v15 = 0;
        boolean v14 = false;
        String v13 = null;
        String v12 = null;
        String v11 = null;
        String v10 = null;
        boolean v9 = false;
        String v8 = null;
        String v7 = null;
        String v6 = null;
        String v5 = null;
        String v4 = null;
        if (v23 != null) {
            v22 = v23.getDataString();
            v21 = v23.getBooleanExtra("show_toolbar", false);
            this.k = v23.getBooleanExtra("use_swipe", false);
            this.l = v23.getIntExtra("swipe_mode", 0);
            v20 = v23.getStringExtra("referer");
            this.m = v23.getIntExtra("orientation", 1);
            v23.getStringExtra("screen_name");
            v23.getStringExtra("screen_context");
            v18 = v23.getLongExtra("ad_id", 0);
            if (v18 > 0) {
                this.m = 0;
            }

            v17 = v23.getStringExtra("bundle_app_package_name");
            v14 = v23.getBooleanExtra("bundle_user_webview_title", false);
            v16 = v23.getBooleanExtra("bundle_is_from_app_ad", false);
            v15 = v23.getIntExtra("bundle_app_ad_from", 0);
            v13 = v23.getStringExtra("bundle_download_url");
            v12 = v23.getStringExtra("bundle_download_app_name");
            v11 = v23.getStringExtra("bundle_download_app_extra");
            v10 = v23.getStringExtra("bundle_download_app_log_extra");
            v9 = v23.getBooleanExtra("bundle_no_hw_acceleration", false);
            v8 = v23.getStringExtra("gd_label");
            v7 = v23.getStringExtra("gd_ext_json");
            v6 = v23.getStringExtra("webview_track_key");
            v5 = v23.getStringExtra("wap_headers");
            this.n = v23.getBooleanExtra("hide_more", false);
            this.a = v23.getStringExtra("back_button_color");
            if (android.support.design.a.isTextEmpty(this.a)) {
                this.a = "black";
            }

            this.b = v23.getStringExtra("back_button_icon");
            if (android.support.design.a.isTextEmpty(this.b)) {
                this.b = "back_arrow";
            }

            v4 = v23.getStringExtra("back_button_position");
            this.g = v23.getBooleanExtra("back_button_disable_history", false);
            v2 = v23.getBooleanExtra("key_hide_bar", this.b());
            v3 = v23.getBooleanExtra("bundle_forbid_jump", false);
            v27 = v6;
            v6 = v8;
            v8 = v7;
            v7 = v27;
        } else {
            v2 = false;
            v3 = false;
            v27 = v6;
            v6 = v8;
            v8 = v7;
            v7 = v27;
        }

        this.a(this.m);
        this.e = new ArrayList();
        this.e.add(OperationButton.REFRESH);
        this.e.add(OperationButton.COPYLINK);
        this.e.add(OperationButton.OPEN_WITH_BROWSER);
        this.e.add(OperationButton.SHARE);
        super.init();
        this.b(this.b);
        this.a(this.a);
        this.c(v4);
        if (this.h) {
            ((ViewGroup.MarginLayoutParams)this.p.getLayoutParams()).topMargin = this.getImmersedStatusBarHelper().c();
            this.p.requestLayout();
            v2 = true;
        }

        if (v2) {
            this.mTitleBar.setBackgroundResource(com.ss.android.article.browser.R.drawable.e);
            UIUtils.setVisibility(this.mRightBtn, 8);
            UIUtils.setVisibility(this.mTitleView, 8);
            UIUtils.setVisibility(this.o, 8);
            ((RelativeLayout.LayoutParams)this.mSwipeOverlay.getLayoutParams()).addRule(3, 0);
        }

        String v2_1 = null;
        if (v23 != null) {
            v2_1 = v23.getStringExtra("title");
        }

        if (android.support.design.a.isTextEmpty(v2_1)) {
            v2_1 = this.getString(com.ss.android.article.browser.R.string.ss_title_browser);
        }

        this.mTitleView.setText(((CharSequence) v2_1));
        this.mRightBtn.setOnClickListener(this.q);
        if (!TTUtils.isValidUrl(v22)) {
            this.finish();
            return;
        }

        Bundle v4_1 = new Bundle();
        v4_1.putString("bundle_url", v22);
        v4_1.putBoolean("show_toolbar", v21);
        v4_1.putBoolean("bundle_user_webview_title", v14);
        if (!android.support.design.a.isTextEmpty(v7)) {
            v4_1.putString("webview_track_key", v7);
        }

        if (!android.support.design.a.isTextEmpty(v20)) {
            v4_1.putString("referer", v20);
        }

        if (v18 > 0) {
            v4_1.putLong("ad_id", v18);
        }

        if (!android.support.design.a.isTextEmpty(v17)) {
            v4_1.putString("package_name", v17);
        }

        if (v9) {
            v4_1.putBoolean("bundle_no_hw_acceleration", v9);
        }

        if (!android.support.design.a.isTextEmpty(v6)) {
            v4_1.putString("gd_label", v6);
        }

        if (!android.support.design.a.isTextEmpty(v8)) {
            v4_1.putString("gd_ext_json", v8);
        }

        if (!android.support.design.a.isTextEmpty(v10)) {
            v4_1.putString("bundle_download_app_log_extra", v10);
        }

        if ((v16) && !android.support.design.a.isTextEmpty(v13)) {
            v4_1.putString("bundle_download_url", v13);
            v4_1.putString("bundle_download_app_name", v12);
            v4_1.putBoolean("bundle_is_from_app_ad", v16);
            v4_1.putInt("bundle_app_ad_from", v15);
            v4_1.putString("bundle_download_app_extra", v11);
        }

        v4_1.putBoolean("bundle_forbid_jump", v3);
        if (!android.support.design.a.isTextEmpty(v5)) {
            v4_1.putString("wap_headers", v5);
        }

        e v2_2 = this.a();
        this.c = new WeakReference(v2_2);
        v2_2.l();
        FragmentTransaction v3_1 = this.getSupportFragmentManager().beginTransaction();
        if ((v2_2 instanceof Fragment)) {
            ((Fragment) v2_2).setArguments(v4_1);
            if (this.getSupportFragmentManager().findFragmentByTag("browser_fragment_tag") == null) {
                v3_1.add(com.ss.android.article.browser.R.id.browser_fragment, ((Fragment) v2_2), "browser_fragment_tag");
            } else {
                v3_1.replace(com.ss.android.article.browser.R.id.browser_fragment, ((Fragment) v2_2), "browser_fragment_tag");
            }
        }

        v3_1.commit();
        if (!android.support.design.a.isTextEmpty(v6)) {
            JSONObject v2_3 = null;
            if (!android.support.design.a.isTextEmpty(v8)) {
                try {
                    v7_1 = new JSONObject(v8);
//                    goto label_381;
                } catch (Exception v3_2) {
                }
            } else {
                v7_1 = v2_3;
            }
//        label_381:
            Object v2_4 = ComponentsManifest.findComponent(com.ss.android.aa.class);
            if (v2_4 == null) {
//                goto label_388;
            } else {
                ((com.ss.android.aa) v2_4).a(this.getApplicationContext(), "wap_stat", "wap_enter", v6, v7_1);
            }
        }

        label_388:
        if (this.n) {
            this.mRightBtn.setVisibility(View.INVISIBLE);
        }
    }

    public final void disableOverlaySwipe() {
        if (this.mSwipeOverlay != null) {
            this.mSwipeOverlay.setSwipeEnabled(false);
        }
    }

    public void onBackBtnClick() {
        this.finish();
    }

    public void onBackPressed() {
        if (this.g) {
            this.onBackBtnClick();
        } else {
            if (this.d.getVisibility() != View.VISIBLE) {
                this.d.postDelayed(new com.ss.android.newmedia.activity.browser.c(this), 300);
            }

            WebView v0 = this.d();
            if (v0 != null && (v0.canGoBack())) {
                v0.goBack();
                return;
            }

            this.onBackBtnClick();
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        boolean v0 = false;
        this.requestDisableOptimizeViewHierarchy();
        if ((this.getIntent().getBooleanExtra("hide_status_bar", false)) && Build.VERSION.SDK_INT >= 23) {
            v0 = true;
        }

        this.h = v0;
        this.j = this.getIntent().getStringExtra("status_bar_background");
        this.i = this.getIntent().getStringExtra("status_bar_color");
        super.onCreate(savedInstanceState);
        this.getWindow().setSoftInputMode(48);
    }

    public boolean useSwipe() {
        boolean v0 = true;
        if (!this.k && this.l != 1 && this.l != 2) {
            v0 = false;
        }

        return v0;
    }

    public boolean useSwipeRight() {
        boolean v0 = true;
        if (this.l == 1) {
            v0 = false;
        }

        return v0;
    }
}

